package cn.edu.scau.cmi.zhonghaozheng.SingletonPattern;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class SingletonGUI {
	private String history = "";
	public SingletonGUI(Stage primaryStage) {
		final Label outputLabel = new Label();
		final Label warningLabel = new Label();
		final TextField inputName = new TextField();
		final Button createBtn1 = new Button();
		final Button createBtn2 = new Button();
        primaryStage.setTitle("Singleton Pattern");
	    inputName.setPromptText("输入名字");
	    inputName.setPrefColumnCount(10);//20
	    inputName.setLayoutX(30);
        outputLabel.setLayoutX(20);
        outputLabel.setLayoutY(70);
        warningLabel.setLayoutX(30);
        warningLabel.setLayoutY(30);
        warningLabel.setTextFill(Color.RED);
        createBtn1.setLayoutX(300);
        createBtn1.setText("主席");
        createBtn1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
            	warningLabel.setText("");
            	try {
                	String cn = inputName.getText();
            		chairman cm = chairman.getInstance(cn);
            		history += cm.getName()+"\n"+cm.toString()+"\n";
                    outputLabel.setText(history);
            	}
            	catch(NullPointerException e){
            		warningLabel.setText("？？");
            	}
            }
        });
        createBtn2.setLayoutX(400);
        createBtn2.setText("元帅");
        createBtn2.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
            	warningLabel.setText("");
            	try {
            		String cn = inputName.getText();
            		Marshal m = Marshal.getInstance(cn);
            		history += m.getName()+"\n"+m.toString()+"\n";
            		outputLabel.setText(history);
            	}
            	catch(NullPointerException e){
            		warningLabel.setText("��������Ϊ��");
            	}
            	catch(ArrayIndexOutOfBoundsException e) {
            		warningLabel.setText("�Ѵ���10��Ԫ˧");
            	}
            }
        });
        Group root = new Group();
        root.getChildren().add(createBtn1);
        root.getChildren().add(createBtn2);
        root.getChildren().add(outputLabel);
        root.getChildren().add(warningLabel);
        root.getChildren().add(inputName);
        primaryStage.setScene(new Scene(root, 500, 500));
        primaryStage.show();
	}
}
